package com.example.xd.bilbil

import android.content.Intent
import android.media.MediaPlayer
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus


class MainActivity : AppCompatActivity(), View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.i("xd----", "onCreate")
        small_btn.setOnClickListener(this)
        btn_list.setOnClickListener(this)
        btn_jump.setOnClickListener(this)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(applicationContext)) {
                //启动Activity让用户授权
                val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                intent.data = Uri.parse("package:$packageName")
                startActivityForResult(intent, 100)
            }
        }
        video.setVideoPath("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        video.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
            override fun onPrepared(mp: MediaPlayer) {
                video.start()
                val duration = video.duration
                Log.i("xd--------", "duration----" + duration)
            }

        })
    }

    override fun onRestart() {
        super.onRestart()
        val current_time = SPUtils.getInstance(this).getInt("time", 0)
        video.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
            override fun onPrepared(mp: MediaPlayer) {
                video.seekTo(current_time)
                video.start()
            }
        })

    }

    override fun onStart() {
        super.onStart()
        Log.i("xd----", "onStart")

    }

    override fun onResume() {
        super.onResume()
        Log.i("xd----", "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.i("xd----", "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.i("xd----", "onStop")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("xd----", "onDestroy")

    }

    override fun onClick(v: View?) {
        when (v) {
            small_btn -> {
                SPUtils.getInstance(this).put("time", video.currentPosition)
                video.pause()
                moveTaskToBack(true)
                FloatWindowManager.getInstance().applyOrShowFloatWindow(MainActivity@ this)
            }
            btn_jump -> {
                EventBus.getDefault().postSticky(VideoMag(video))
                startActivity(Intent(this@MainActivity, SecondActivity::class.java))
            }
            btn_list -> {
                startActivity(Intent(this@MainActivity, ListActivity::class.java))
            }
        }
    }

}
